package ac.zlf.maven.plugin.spring_mybatis_template.repaid;

import cn.org.rapid_framework.generator.GeneratorFacade;
import cn.org.rapid_framework.generator.GeneratorProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;


/**
 * Created by Administrator on 2018/6/21.
 */
public class Gen {

    private static Logger logger = LoggerFactory.getLogger(Gen.class);

    public static final Map<String, String> templates = new HashMap<>();
    static {
        String jarPath = Gen.class.getProtectionDomain().getCodeSource().getLocation().getPath();

        //Bean、BeanMapper、BeanMapper.xml、Service、ServiceImpl
        templates.put("spring_mybatis_to_service", jarPath+ "!/template_service");
        //fxml、view.java、controller.java
        templates.put("spring_boot_fx_to_view", jarPath+ "!/template_spring_boot_fx");
    }


    public static void generator(String configurationFile, String templatePath) {
        logger.info("------------------load config------------------");
        logger.info("------------------%S------------------", templatePath);
        try {
            GeneratorProperties.load(new String[]{configurationFile});

            GeneratorFacade g = new GeneratorFacade();
            g.getGenerator().addTemplateRootDir(templatePath);
//            g.deleteOutRootDir();

            String tables = GeneratorProperties.getProperty("tableNames");
            if(tables == null && "".equals(tables.trim())){
                logger.error("------------------tableNames = null ------------------");
                return;
            }

            String[] tableArr = tables.trim().split(",");
            for (int i = 0; i < tableArr.length; i++) {
                tableArr[i] = tableArr[i].trim();
            }

            logger.info("------------------generator start------------------");
            g.generateByTable(tableArr);
            logger.info("------------------generator end------------------");
        } catch (InvalidPropertiesFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
